package org.csu.mypetstorecmsapi.service;

import org.csu.mypetstorecmsapi.domain.*;
import org.csu.mypetstorecmsapi.persistence.CategoryDao;
import org.csu.mypetstorecmsapi.persistence.InventoryDao;
import org.csu.mypetstorecmsapi.persistence.ItemDao;
import org.csu.mypetstorecmsapi.persistence.ProductDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CatalogService
{
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private ItemDao itemDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private InventoryDao inventoryDao;

    //search
    public List<Category> getCategories(){return categoryDao.findAll();}

    public List<Product> getProducts(String categoryId){return productDao.findByCategoryId(categoryId);}

    public List<Item> getItems(String productId){
        List<Item> itemList = itemDao.findByProductId(productId);
        for (Item item : itemList){
            item.setQuantity(inventoryDao.findByItemId(item.getItemId()).getQuantity());
            item.setListPrice(item.getConvertedListPrice());
            item.setUnitCost(item.getConvertedUnitCost());
        }
        return itemList;
    }

    public Category getCategoryById(int id){return categoryDao.findById(id).get();}
    public Product getProductById(int id){return productDao.findById(id).get();}
    public Item getItemById(int id){
        Item item = itemDao.findById(id).get();
        item.setQuantity(inventoryDao.findByItemId(item.getItemId()).getQuantity());
        item.setListPrice(item.getConvertedListPrice());
        item.setUnitCost(item.getConvertedUnitCost());
        return item;
    }

    //add
    public void addCategory(Category category){categoryDao.save(category);}
    public void addProduct(Product product){productDao.save(product);}
    public void addItem(Item item){
        item.setConvertedListPrice(item.getListPrice());
        item.setConvertedUnitCost(item.getUnitCost());
        itemDao.save(item);
        Inventory inventory = new Inventory();
        inventory.setItemId(item.getItemId());
        inventory.setQuantity(item.getQuantity());
        inventoryDao.save(inventory);
    }

    //delete
    public void deleteCategory(int id)
    {
        categoryDao.deleteById(id);
    }
    public void deleteProduct(int id)
    {
        productDao.deleteById(id);
    }
    public void deleteItem(int id)
    {
        inventoryDao.deleteByItemId(itemDao.findById(id).get().getItemId());
        itemDao.deleteById(id);
    }
    public void deleteProductByCategoryId(String categoryId){
        productDao.deleteByCategoryId(categoryId);
    }
    public void deleteItemByProductId(String productId){
        List<Item> itemList = itemDao.findByProductId(productId);
        for (Item item : itemList){
            inventoryDao.deleteByItemId(item.getItemId());
        }
        itemDao.deleteByProductId(productId);
    }
    public void deleteItemByCategoryId(String categoryId){
        List<Product> productList = productDao.findByCategoryId(categoryId);
        for (Product product : productList) {
            String productId = product.getProductId();
            List<Item> itemList = itemDao.findByProductId(productId);
            for (Item item : itemList){
                inventoryDao.deleteByItemId(item.getItemId());
            }
            itemDao.deleteByProductId(product.getProductId());
        }
    }

    //edit
    public void editItem(int id, Item i)
    {
        itemDao.findById(id).ifPresent(item -> {
            item.setItemId(i.getItemId());
            item.setProductId(i.getProductId());
            item.setConvertedListPrice(i.getListPrice());
            item.setConvertedUnitCost(i.getUnitCost());
            item.setSupplierId(i.getSupplierId());
            item.setStatus(i.getStatus());
            item.setAttribute1(i.getAttribute1());
            item.setAttribute2(i.getAttribute2());
            item.setAttribute3(i.getAttribute3());
            item.setAttribute4(i.getAttribute4());
            item.setAttribute5(i.getAttribute5());
            item.setQuantity(i.getQuantity());
            itemDao.save(item);
        });

        Inventory inventory = inventoryDao.findByItemId(i.getItemId());
        inventory.setItemId(i.getItemId());
        inventory.setQuantity(i.getQuantity());
        inventoryDao.save(inventory);
    }
}
